<?php

namespace App\Http\Controllers\baseinfo;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\channel;

class ChannelController extends Controller
{
    protected $name = '组织机构档案';
    protected $field = ['channelid', 'name', 'shortname', 'treeid', 'parentid', 'chinaladdress', 'engaddress', 'playtaxid', 'isvirtual', 'isuse', 'des', 'des1', 'des2'];
    /**
     * 保存
     */
    public function save(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            $isnew = false;
            if (empty($id)) {
                $isnew = true;
            }
            $list = [];
            if (empty($param)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存参数为空']);
            }
            foreach ($param as $key => $value) {
                if (in_array($key, $this->field)) {
                    $list[$key] = $value;
                }
            }
            if (empty($list)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|没有需要处理的数据']);
            }
            if ($isnew) {
                $type = '新增';
                if (empty($list['channelid'])) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => '新增失败,机构ID不能为空!']);
                }
                if (empty($list['name'])) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => '新增失败,机构名称不能为空!']);
                }
                $FSQL = "  select * from channel where isdel=0 and channelid='" . $list['channelid'] . "'";
                if (!empty(DB::select($FSQL))) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '机构ID已经存在,不可重复!']);
                }
                if (!empty($list['parentid'])) {
                    //如果不是根节点
                    $FSQL = "  select * from channel where isdel=0 and treeid='" . $list['parentid'] . "'";
                    if (empty(DB::select($FSQL))) {
                        return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|父节点不存在,不能保存!']);
                    }
                    $FSQL = " select MAX(treeid) as maxtree from channel where isdel=0 and parentid=" . intval($list['parentid']);
                    $treeid = DB::select($FSQL)[0]->maxtree + 1;
                    if ($treeid == 1) {
                        $treeid = $list['parentid'] . '01';
                    }
                } else {
                    $FSQL = " select MAX(treeid) as maxtree from channel where isdel=0 and parentid is null";
                    $treeid = DB::select($FSQL)[0]->maxtree + 1;
                }

                $list['treeid'] = $treeid;
                //新增
                $res = channel::create($list);
                if (empty($res)) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '新增失败!']);
                }
            } else {
                $type = '修改';
                $uptab = channel::find($id);
                $uptab->update($list);
                $res = $uptab;
            }
            $FSQL = "SELECT id,channelid,name,treeid,parentid,shortname,chinaladdress,engaddress,playtaxid,isvirtual,isuse,des,tabnm,modifier
            FROM channel where isdel=0";
            $data = DB::select($FSQL);
            //记录日志
            $this->log(['type' => $type, 'billcaption' => '组织机构', 'des' => '操作人[' . $type . ']了组织[' . $list['name'] . '],数据为|[' . json_encode($list) . ']']);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|保存成功!', 'data' => $data, 'nowdata' => $res]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 查询
     */
    public function find(Request $request)
    {
        try {
            //$posts = Post::where('title', 'like', '%Post%')->get();
            $param = json_decode($request->param, true);
            $channelid = $this->I($param['channelid'] ?? null);
            $name = $this->I($param['name'] ?? null);
            $where = ' isdel=0';
            if (!empty($name)) {
                $where .= ' and name like "%' . $name . '%"';
            }
            if (!empty($channelid)) {
                $where .= "and channelid = '$channelid'";
            }
            $FSQL = "SELECT COUNT(1) as allcount FROM Channel a where  $where";
            $allcount = DB::select($FSQL)[0]->allcount;

            $FSQL = "SELECT id,channelid,name,treeid,parentid,shortname,chinaladdress,engaddress,playtaxid,isvirtual,isuse,des,tabnm,modifier
              FROM Channel where   $where";
            $data = DB::select($FSQL);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'count' => $allcount, 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            if (empty($id)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!机构ID为空']);
            }

            $uptab = channel::find($id); // 找到 ID 为 1 的记录
            if (empty($uptab)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!机构不存在']);
            }
            $uptab->isdel = 1;
            $res = $uptab->save();
            if (empty($res)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!']);
            }
            $FSQL = "SELECT  id,channelid,name,treeid,parentid,shortname,chinaladdress,engaddress,playtaxid,isvirtual,isuse,des,tabnm,modifier
              FROM Channel  where isdel=0";
            $data = DB::select($FSQL);
            //记录日志
            $this->log(['type' => '删除', 'billcaption' => '组织机构', 'des' => '操作人[删除]了组织[' . $uptab->name . '],ID为|[' . $uptab->channelid . ']']);
            return $this->ajaxReturn(['code' => 1, 'msg' => '删除成功!', 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|删除失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 首页加载组织机构
     */
    public function loadchannel(Request $request)
    {
        try {
            $FSQL = "SELECT channelid as id,name
              FROM Channel where isdel <>1";
            $data = DB::select($FSQL);
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }
}
